﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.Common.Public.Enums;
using RFIDStockManagement.BLL;
using DevExpress.XtraBars;

namespace RFIDStockManagement.Client
{
    public partial class StockEdit : XtraForm
    {
        private StockInfo m_StockInfo = null;
        private long m_Id = 0;
        public StockEdit(long id)
        {
            InitializeComponent();
            m_Id = id;
        }

        private void StockEdit_Load(object sender, EventArgs e)
        {            
            if (m_Id > 0)
            {
                BindData(m_Id);
            }
        }
        private void BindData(long id)
        {
            try
            {
                m_StockInfo = BasicInfoManager.GetOne(id, EnumBasicInfo.Stock) as StockInfo;
                if (m_StockInfo == null)
                {
                    AlertForm.ShowMessage("读取不出数据，可能被删除");
                    this.Close();
                    return;
                }
                txtName.Text = m_StockInfo.Name;
                txtNumber.Text = m_StockInfo.Number;
                cbbType.SelectedIndex = ((int)m_StockInfo.StockType) - 1;
                gridData.DataSource = m_StockInfo.StockPlaceList;
                this.btnAdd.Enabled = true;
                this.btnDelete.Enabled = true;
                this.btnEdit.Enabled = true;
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
                this.Close();
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Trim().Length == 0 || txtNumber.Text.Trim().Length == 0)
                {
                    AlertForm.ShowMessage("单位组名称和编码都不能为空");
                    return;
                }
                if (m_StockInfo == null)
                {
                    m_StockInfo = new StockInfo();
                }
                m_StockInfo.Name = txtName.Text.Trim();
                m_StockInfo.Number = txtNumber.Text.Trim();
                m_StockInfo.StockType = (EnumStockType)(cbbType.SelectedIndex + 1);

                m_StockInfo = BasicInfoManager.Save(m_StockInfo, EnumBasicInfo.Stock) as StockInfo;
                m_Id = m_StockInfo.Id;
                this.btnAdd.Enabled = true;
                this.btnDelete.Enabled = true;
                this.btnEdit.Enabled = true;
                AlertForm.ShowMessage("保存成功");
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            StockPlaceEdit stockPlaceEdit = new StockPlaceEdit(m_Id, 0);
            stockPlaceEdit.ShowDialog();
            BindStockPlaceList();
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                int[] selectRows = gridView.GetSelectedRows();
                if (selectRows != null && selectRows.Length > 0)
                {
                    DataRowView rowView = gridView.GetRow(selectRows[0]) as DataRowView;
                    if (rowView != null)
                    {
                        long id = long.Parse(rowView.Row["FID"].ToString());
                        StockPlaceEdit stockPlaceEdit = new StockPlaceEdit(m_Id, id);
                        stockPlaceEdit.ShowDialog();
                        BindStockPlaceList();
                    }
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int[] selectRows = gridView.GetSelectedRows();
                if (selectRows == null || selectRows.Length == 0)
                {
                    AlertForm.ShowMessage("至少要选择一行才能删除");
                    return;
                }

                if (AlertForm.Confirm("确定删除所有选择的数据？"))
                {
                    List<string> lstId = new List<string>();
                    DataRowView rowView = gridView.GetRow(selectRows[0]) as DataRowView;
                    lstId.Add(rowView.Row["FID"].ToString());
                    List<string> result = BasicInfoManager.Delete(lstId, EnumBasicInfo.StockPlace);
                    if (result.Count == 1)
                    {
                        AlertForm.ShowMessage(result[0]);
                    }
                    else
                    {
                        AlertForm.ShowMessage(result);
                    }
                    BindStockPlaceList();
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }
        private void BindStockPlaceList()
        {
            if (m_Id > 0)
            {
                Dictionary<string, object> filter = new Dictionary<string, object>();
                filter.Add("FStockID", m_Id);
                this.gridData.DataSource = BasicInfoManager.GetList(filter, EnumBasicInfo.StockPlace);
            }
        }
    }
}